package com.example.comment_oa.util;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.comment_oa.pojos.SysUser;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 读取excel监听器
 * @author cjl
 */
public class AccountListener extends AnalysisEventListener{
    /**
     * 没读完一行内容，调用的方法
     * @param
     * @param analysisContext
     */

    private BaseMapper sysDeptMapper;

    public AccountListener(BaseMapper sysDeptMapper){
        this.sysDeptMapper = sysDeptMapper;
    }

    List list = new ArrayList<>();

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        list.add(o);
    }

    /**
     * 读取完整个文档后调用的方法
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        list.forEach(e->{
            sysDeptMapper.insert(e);
        });
    }
//    private void saveData(){
//        System.out.println("{" + list.size() + "}条数据，开始存储数据库" );
//        for (SysUser dept : list) {
//            System.out.println(dept);
//            //sysDeptMapper.insert(dept);
//        }
//        System.out.println("存储数据库成功");
//    }
}
